package cn.zlj.nmcc.service.impl;

import cn.zlj.nmcc.constanst.SmsCodeConstants;
import cn.zlj.nmcc.constants.BaseConstants;
import cn.zlj.nmcc.domain.Blacklist;
import cn.zlj.nmcc.domain.LoginDto;
import cn.zlj.nmcc.dto.RedisDto;
import cn.zlj.nmcc.exception.GlobalException;
import cn.zlj.nmcc.exception.GlobalExceptionCodeEnum;
import cn.zlj.nmcc.result.JSONResult;
import cn.zlj.nmcc.service.BlackListService;
import cn.zlj.nmcc.service.VerifyCodeService;
import cn.zlj.nmcc.util.AssertUtil;
import cn.zlj.nmcc.util.StrUtils;
import cn.zlj.nmcc.util.VerifyCodeUtils;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.concurrent.TimeUnit;

@Service
public class VerifyCodeServiceImpl implements VerifyCodeService {

    @Autowired
    private RedisTemplate<Object, Object> redisTemplate;
    @Autowired
    private SmsCodeConstants smsCodeConstants;
    @Autowired
    private BlackListService blackListService;
    @Override
    public String verifyCode(String key) {
        if(StringUtils.isBlank(key)){
            throw new GlobalException("uuid异常");
        }
        String random = StrUtils.getRandomString(4);
        redisTemplate.opsForValue().set(key,random);
        VerifyCodeUtils verifyCodeUtils = new VerifyCodeUtils();
        String base64 = verifyCodeUtils.verifyCode(100, 30,random );
        return base64;
    }

    @Override
    public void sendSmsCode(LoginDto loginDto) {
      //参数校验用jsr
        //判断图形验证码是否正确
        Object o = redisTemplate.opsForValue().get(loginDto.getImageCodeKey());
        //redis中图形验证码存在
        if(o==null||!loginDto.getImageCode().equals(o)){
           throw new GlobalException("图形验证码错误");
        }
        //判断手机号是否在黑名单中
        JSONResult list = blackListService.list();
        List<Blacklist> blacklist = JSONObject.parseArray(JSONObject.toJSONString(list.getData()), Blacklist.class);
        blacklist.forEach(blacklist1->{
          if(blacklist1.equals(loginDto.getMobile())) throw new GlobalException("在黑名单中");
        });


        //判断redis中是已经有短信验证码了
        String code="";
        Long nowTime = System.currentTimeMillis();
        String key=String.format(BaseConstants.VerifyCodeConstants.REGISTER_send_SmsCode, loginDto.getMobile());
        Object o1 = redisTemplate.opsForValue().get(key);
        RedisDto redisDto =(RedisDto)o1;
        if(o1!=null){
            if(nowTime-Long.valueOf(redisDto.getTime())> smsCodeConstants.getInterval()){
                System.out.println(redisDto.getCode());
                throw new GlobalException(GlobalExceptionCodeEnum.UAA_PARAM_IS_exists);
            }
            code=redisDto.getCode();
        }else {
            code=StrUtils.getRandomString(smsCodeConstants.getCodesize());
        }
        //如果没有验证码则自己生成放入redis然后发送
        RedisDto dto = new RedisDto(code, nowTime);
        redisTemplate.opsForValue().set(key,dto,smsCodeConstants.getTimeout(), TimeUnit.SECONDS);
        System.out.println(dto.getCode());
    }

}
